package com.ajaxlord.confluence.plugin.json.actions;

import com.atlassian.confluence.user.UserPreferencesKeys;
import com.atlassian.confluence.util.GeneralUtil;
import com.atlassian.user.User;
import com.opensymphony.module.propertyset.PropertyException;
import com.opensymphony.module.propertyset.PropertySet;
import org.apache.commons.lang.StringUtils;

import java.util.Date;

public class ProfileAction extends JSONActionSupport {
    private String username;
    private User user;

    public String getUsername() {
        if ((username == null || StringUtils.isEmpty(username)) && getRemoteUser() != null)
            username = getRemoteUser().getName();
        return username;
    }

    public void setUsername(String username) {
        if (GeneralUtil.shouldUrlDecode(username))
            username = GeneralUtil.urlDecode(username);

        this.username = username;
    }

    public User getUser() {
        if ((user == null) && StringUtils.isNotEmpty(getUsername()))
            user = userAccessor.getUser(getUsername());

        return user;
    }

    public boolean isViewingMyProfile() {
        return (getUser() != null && getUser().equals(getRemoteUser()));
    }

    public boolean isEmailVisible() {
        return isViewingMyProfile() || super.isEmailVisible();
    }

    public String renderEmail(String email) {
        return GeneralUtil.maskEmail(email);
    }

    public Date getSignupDate() {
        try {
            return getDateProperty(UserPreferencesKeys.PROPERTY_USER_SIGNUP_DATE);
        }
        catch (Exception e) {
            log.error("An error occurred trying to retrieve signup date");
            e.printStackTrace();
        }

        return null;
    }

    public Date getLastLoginDate() {
        try {
            return getDateProperty(UserPreferencesKeys.PROPERTY_USER_PREVIOUS_LOGIN_DATE);
        }
        catch (Exception e) {
            log.error("An error occurred trying to retrieve last login date");
            e.printStackTrace();
        }

        return null;
    }

    /**
     * A backward compatibility method
     * <p/>
     * 'signup' and 'last login' date properties are now actual Date objects but were stored as longs before.
     * This method checks the type of the property before fetching the value with the appropriate getter method
     *
     * @param propertyKey The property key.
     * @return The date stored in the property.
     */
    private Date getDateProperty(String propertyKey) {
        PropertySet propertySet = userAccessor.getPropertySet(getUser());
        if (propertySet.exists(propertyKey)) {
            try {
                long aLong = propertySet.getLong(propertyKey);
                if (aLong > 0)
                    return new Date(aLong);
                else // really ugly, but no way around it. getPropertySet().getType(key) is broken, so we can't check for types
                    return propertySet.getDate(propertyKey);
            }
            catch (PropertyException e) {
                return propertySet.getDate(propertyKey);
            }
        } else
            return null;
    }

}
